package com.hmall.search.web;

import com.hmall.search.dto.searchfilters;
import com.hmall.search.pojo.Item;
import com.hmall.search.pojo.PageDTO;
import com.hmall.search.pojo.searchf;
import com.hmall.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description SearchController
 * @Author liujiangyuan
 * @Date 2024-11-15-11:44
 */
@RestController
@RequestMapping("search")
public class SearchController {
    @Autowired
    private SearchService searchService;
    private static final String TOP_20_SOLD_ITEMS_KEY = "top_20_sold_items";

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/filters")
    public searchf searchfiter(searchfilters searchfilters){
        searchf searchf = searchService.searchfiter(searchfilters);
        return searchf;
    }
    @PostMapping("/list")
    public PageDTO searchlist(@RequestBody searchfilters searchfilters){
        PageDTO pageDTO=searchService.searchlist(searchfilters);
        List<Item> items = redisTemplate.opsForList().range(TOP_20_SOLD_ITEMS_KEY, 0, -1);
        if (null==searchfilters.getBrand()&&
                null==searchfilters.getCategory()&&
                null==searchfilters.getMinPrice()&&
                null ==searchfilters.getMaxPrice()&&
                1==searchfilters.getPage()&&
        "default".equals(searchfilters.getSortBy()))
        {
            pageDTO.setList(items);
        }else {
        return pageDTO;
    }
        return pageDTO;
}
}
